<template>
  <div>
    <!-- 表单 -->
    <el-form ref="form" :model="music" label-width="80px">
      <el-form-item label="歌名">
        <el-input v-model="music.name" />
      </el-form-item>
      <el-form-item label="艺术家">
        <el-input v-model="music.artist" />
      </el-form-item>

      <!-- 文件上传 -->
      <el-form-item label="歌曲文件">
        <!-- <el-input v-model="music.url" /> -->
        <el-upload
          :action="uploadUrl"
          :headers="headers"
          :on-success="uploadMusicSuccess"
          :limit="1"
          :file-list="fileList"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
        </el-upload>
      </el-form-item>

      <el-form-item label="封面">
        <!-- <el-input v-model="blog.blogImage" /> -->
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :headers="headers"
          :on-success="uploadSuccess"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>

      <el-form-item label="歌词">
        <el-input v-model="music.lrc" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即添加</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// 引入接口API
import musicApi from '@/api/music'
// import Tinymce from '@/components/Tinymce/index'
import { getToken } from '@/utils/auth'
export default {
  data() {
    return {
      music: {},
      imageUrl: null, // 上传图片回显
      // 设置上传文件的请求头
      headers: {
        Authorization: getToken()
      },
      fileList: [], // 上传文件的文件列表
      // 从全局变量中去数据
      uploadUrl: process.env.VUE_APP_UPLOAD_URL, // 上传图片路径
      typeList: this.$store.getters.typeList
    }
  },
  methods: {
    onSubmit() {
      musicApi.save(this.music).then(Response => {
        this.$message.success(Response.msg)
        this.$emit('closeAddDialog')
        this.$emit('getmusicList')
      })
    },
    uploadSuccess(res, file) {
      this.$message.success(res.msg)
      // 图片回显
      this.imageUrl = res.data
      // 图片路径提交
      this.music.cover = res.data
    },
    uploadMusicSuccess(res, file) {
      this.$message.success(res.msg)
      // 音乐文件回显
      this.imageUrl = res.data
      // 音乐路径提交
      this.music.url = res.data
    },
    handlePreview(file) {
      console.log(file)
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

